//
//  PostBidsDetailViewController.m
//  Globalstone
//
//  Created by 刘建峰 on 16/7/18.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PostBidsDetailViewController.h"
#import "PostBidsDetailTableViewCell.h"
#import "PayTypeViewController.h"

static NSString * cellIndentifier = @"PostBidsDetailTableViewCell";
@interface PostBidsDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UILabel *headTitle;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *postButton;
@property(nonatomic,strong)NSMutableArray * bidsArray;


@property (weak, nonatomic) IBOutlet UIImageView *companyIMage;
@property (weak, nonatomic) IBOutlet UILabel *bidName;
@property (weak, nonatomic) IBOutlet UILabel *companyName;

@property (weak, nonatomic) IBOutlet UILabel *companyPerson;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectMoney;
@property (weak, nonatomic) IBOutlet UILabel *projectAddress;
@property (weak, nonatomic) IBOutlet UILabel *endTime;
@property (weak, nonatomic) IBOutlet UILabel *serviceRequest;
@property (weak, nonatomic) IBOutlet UILabel *pdfLabel;

@property(nonatomic,strong)NSDictionary * bidsDetailDic;

@end

@implementation PostBidsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _bidsArray = [NSMutableArray array];
    [self initViewLayout];
    [self getBidDetail];
    if ([_bidStatus integerValue]>=3) {
        [self getBidWinner];
        if ([_bidStatus isEqualToString:@"5"]) {
            _postButton.frame = flexibleFrame(CGRectMake(230, 9, 75, 18), NO);
            [_postButton setTitle:@"已收货去评价" forState:UIControlStateNormal];
        }
    }else{
    [self getBidsList];
    }
    
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    
    _scroView.contentSize = flexibleSize(CGSizeMake(0,505), NO);
    
    _postButton.layer.borderWidth = 1;
    _postButton.layer.cornerRadius = 2*VerticalRatio();
    _postButton.layer.borderColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.000 alpha:1.000].CGColor;

    
    [_tableView registerNib:[UINib nibWithNibName:cellIndentifier bundle:nil] forCellReuseIdentifier:cellIndentifier];
    _tableView.dataSource = self;
    _tableView.delegate = self;

}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark ==== 去付款
- (IBAction)payHandel:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
    PayTypeViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PayType"];
    page.payOrderPrice = _bidsDetailDic[@"orderPrice"];
    page.payOrderName = @"中盘科技";
    page.payId = _bidsDetailDic[@"orderNo"];
//    page.orderFirstNo = database[@"result"][@"orderFirstNo"];
    page.payTypes = @"2";
    [[Common getNavigation]pushViewController:page];

}

#pragma mark === 标书详情
- (void)getBidDetail{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_bidId forKey:@"mmdeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryDemandDetail withHttpType:@"POST" withTag:GS_GS_queryDemandDetail];
    
}

#pragma mark === 标书招标单位列表
- (void)getBidsList{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_bidId forKey:@"mmdeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryMallMarketDemandDetail withHttpType:@"POST" withTag:GS_GS_queryMallMarketDemandDetail];
    
}

#pragma mark === 标书中标单位
- (void)getBidWinner{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_bidId forKey:@"mmdeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_querySupplyedInfo withHttpType:@"POST" withTag:GS_GS_querySupplyedInfo];
    
}

#pragma mark  ==== tableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_bidsDetailDic) {
        return 1;
    }else{
        return _bidsArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 177*VerticalRatio();
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PostBidsDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[PostBidsDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    NSDictionary * dic = [NSDictionary dictionary];
    if (_bidsDetailDic) {
        dic = _bidsDetailDic;
        cell.getBidsButton.hidden = YES;
        cell.headTitle.text = @"中标单位";
    }else{
        dic = _bidsArray[indexPath.row];
    }
    cell.companyName.text = [NSString stringWithFormat:@"%@",dic[@"supplyMstoName"]];
//    cell.giveMoney.text = [NSString stringWithFormat:@"￥%.2f",[dic[@"mmsuPrice"] floatValue]];
    cell.commitTime.text = [NSString stringWithFormat:@"%@",dic[@"mmsuCreateTime"]];
    cell.commitWords.text = [NSString stringWithFormat:@"%@",dic[@"mmsuContent"]];
    
    cell.giveBidBlock = ^(){
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
        [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
        [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
        
        NSString * lan = [InternationalControl userLanguage];
        if ([lan isEqualToString:@"en"]) {
            [dic setObject:@"2" forKey:@"language"];
        }else{
            [dic setObject:@"1" forKey:@"language"];
        }
        [dic setObject:_bidId forKey:@"mmsuDemandId"];
        [dic setObject:dic[@"museId"] forKey:@"mmsuId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:GS_saveMarketOrder withHttpType:@"POST" withTag:GS_GS_saveMarketOrder];

    };
    
    cell.callBlock = ^(){
    NSString * phone =@"";
        if (_bidsDetailDic) {
            phone = [NSString stringWithFormat:@"%@",_bidsDetailDic[@"supplyMstoPhone"]];
        }else{
            phone = [NSString stringWithFormat:@"%@",_bidsArray[indexPath.row][@"supplyMstoPhone"]];
        }
        [Common callPhone:phone view:self.view];
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
//    UIViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"BusinessInfoDetail"];
//    [[Common getNavigation]pushViewController:page];
    
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==GS_GS_queryDemandDetail ) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic = database[@"result"];
            
            _bidName.text = [NSString stringWithFormat:@"%@",dic[@"mmdeName"]];
            _companyPerson.text = [NSString stringWithFormat:@"%@",dic[@"mstoMemberName"]];
            _companyName.text = [NSString stringWithFormat:@"%@",dic[@"mstoName"]];
            
            NSString * image = [NSString stringWithFormat:@"%@",dic[@"mstoImage"]];
            if (image.length>0) {
                [_companyIMage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
            }
            
            _addressLabel.text = [NSString stringWithFormat:@"%@%@",dic[@"citySysArea"][@"AName"],dic[@"proSysArea"][@"AName"]];
            
            NSString * date = [NSString stringWithFormat:@"%@",dic[@"mmdeCreateTime"]];
            if (date.length>=10) {
                date = [date substringToIndex:10];
            }
            _dateLabel.text = date;
            
            _projectMoney.text = [NSString stringWithFormat:@"%@",dic[@"mmdePrice"]];
            _projectAddress.text = [NSString stringWithFormat:@"%@",dic[@"mmdeCity"]];
            
            NSString * endTime = [NSString stringWithFormat:@"%@",dic[@"mmdeDueTime"]];
            if (endTime.length>=10) {
                endTime = [endTime substringToIndex:10];
            }
            _endTime.text = endTime;
            
            _serviceRequest.text = [NSString stringWithFormat:@"%@",dic[@"mmdeContent"]];
            
            NSString * pdf = [NSString stringWithFormat:@"%@",dic[@"mmdeAttach"]];
            if (pdf.length>=26) {
                 pdf = [pdf substringWithRange:NSMakeRange(11, pdf.length-15)];
            }
            _pdfLabel.text = pdf;
        }
    }else if (tag == GS_GS_queryMallMarketDemandDetail){
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _bidsArray = database[@"result"];
            [_tableView reloadData];
            _tableView.frame = flexibleFrame(CGRectMake(0, 313, 320, 177*_bidsArray.count), NO);
            if (CGRectGetMaxY(_tableView.frame)>_scroView.contentSize.height) {
                _scroView.contentSize = flexibleSize(CGSizeMake(0, CGRectGetMaxY(_tableView.frame)), NO);
            }
            
        }
    
    }else if (tag == GS_GS_saveMarketOrder){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
             [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }
    }else if (tag == GS_GS_querySupplyedInfo){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _bidsDetailDic = database[@"result"];
            _postButton.hidden = NO;
            [_postButton setTitle:@"去付款" forState:UIControlStateNormal];
            _headTitle.text = @"中标标书详情";
            [_tableView reloadData];
        }
    
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    [self dismissWaitDialog];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
